<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;

class KindgoodsController extends CommonController{//级构造函数冲突
	private $model;
	private $gattrModel;
	public function __init(){
		//调用CommonController里面的构造函数
		parent::__init();
		//实例化kindgoods模型类
		$this->model = new \Common\Model\Kindgoods;
		$this->gattrModel = new \Common\Model\Goodsattr;
	}
	//货品列表展示
	public function index(){
		$gid = Q('get.gid',0,'intval');	
		$type_typid = Db::table('goods') ->where("gid={$gid}")->pluck('type_typid');		
		//类型属性数据
		$tyattr = Db::table('typeattr')->where("type_typid={$type_typid} and tclass=1 ")->get();																																																																										
		//获得tid
		$tid = Db::table('typeattr')->where("type_typid={$type_typid} and tclass=1 ")->pluck('tid');
		//定义一个空数组
		$attr=array();
		//重组数组
		foreach ($tyattr as $k => $v) {		
			$arr['tname']= $v['tname'];
			$arr['tavalue']=Db::table('goodsattr')->where("typeattr_tid={$v['tid']} AND goods_gid={$gid}")->get();
			$attr[]=$arr;
		}
		View::with('attr',$attr);
		//p($attr);
		//查询货品列表数据用于页面展示
		$kindData = $this->model->where("goods_gid=$gid")->get();
		
		
		foreach ($kindData as $k => $v) {
			//先将字符串拆分成数组
			$arr = explode('|', $v['comid']);
			//p($arr);
			foreach ($arr as $key => $value) {
				//查询商品属性表取得$value（49，52）所对应的属性值再赋值给$arr数组
				$arr[$key] = Db::table('goodsattr')->where("gaid={$value}")->pluck('gvalues');
			}
			//再将$arr数组赋给大数组
			$kindData[$k]['comid'] = $arr;
		}
		//p($kindData);
	
		$spec = Db::table('goodsattr')->where("goods_gid={$gid}")->get();
		View::with('kindData',$kindData);
	    View::make();
	}
	//货品添加
	public function add(){
		if(IS_POST){
			if(!$this->model->store()) View::error($this->model->getError());
			View::success('货品添加成功');
			
		}
	
	}
	//删除货品
	public function del(){
	   $kid = Q('get.kid',0,'intval');
	   $this->model->where("kid={$kid}")->delete();
	   //JS跳转
		$str = <<<str
		<script>
		alert('删除成功');
		history.go(-1);
		</script>
str;
		echo $str;
	}
	//编辑货品
	public function edit(){
		
		if(IS_POST){
			$gid = Q('post.gid');
		
		   if(!$this->model->edit()) View::error($this->model->getError());
		   View::success('编辑成功',U('index',array('gid'=>"$gid")));    
		}
		$kid = Q('get.kid',0,'intval');
		//一：查询货品表总数据
		//获得货品列表里的goods_gid，我就知道了此货品属于哪个商品里，就知道里商品gid
		$gid = $this->model->where("kid={$kid}")->pluck('goods_gid');
		//获得商品表里的$type_typid
		$type_typid = Db::table('goods')->where("gid={$gid}")->pluck('type_typid');
		//根据$type_typid获得类型属性表里的tid , tvalue
		$tyattr = Db::table('typeattr')->where("type_typid={$type_typid} and tclass=1")->get();
		//定义一个空数组，最后组合完的数组旧放到此数组里面
		$attr=array();
		foreach ($tyattr as $k => $v) {
			$arr['tname'] = $v['tname'];
			//循环组合数组
			$arr['tvalue'] = Db::table('goodsattr')->where("typeattr_tid={$v['tid']}")->get();
			//最后把二个$arr小数组压入定义好的大数组里面
			$attr[] = $arr;
		}
		
		View::with('attr',$attr);
		
		//二：查询旧数据
		$oldData = $this->model->where("kid={$kid}")->find();
		//重组数组
		//p($oldData);
		$arr = explode('|', $oldData['comid']);
		$oldData['comid'] = $arr;
		//p($oldData);
		//分配数据
		View::with('oldData',$oldData);
	    View::make();
	}
	
}

?>
	